1
|
|
|
/* |
2
|
|
|
* central function to retrieve state from reducer |
3
|
|
|
* used inside of mapStateToProps by grid and other plugins |
4
|
|
|
* @returns {object} state |
5
|
|
|
|
6
|
|
|
* will not return immutable object, only plain JS object |
7
|
|
|
|
8
|
|
|
* if a dynamic reducerKey is passed, it will favor that key |
9
|
|
|
* over the build in grid keys |
10
|
|
|
|
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
export function stateGetter(state, props, key, entry) { |
14
|
|
|
|
15
|
|
|
if (props |
16
|
|
|
&& props.reducerKeys |
17
|
|
|
&& Object.keys(props.reducerKeys).length > 0 |
18
|
|
|
&& props.reducerKeys[key]) { |
19
|
|
|
|
20
|
|
|
const dynamicKey = props.reducerKeys[key]; |
21
|
|
|
|
22
|
|
|
const dynamicState = state |
23
|
|
|
&& state[dynamicKey] |
24
|
|
|
&& state[dynamicKey].get |
25
|
|
|
&& state[dynamicKey].get(entry) |
26
|
|
|
? state[dynamicKey].get(entry) |
27
|
|
|
: null; |
28
|
|
|
|
29
|
|
|
return dynamicState && |
30
|
|
|
dynamicState.toJS |
31
|
|
|
? dynamicState.toJS() |
32
|
|
|
: dynamicState; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
const firstTry = state |
36
|
|
|
&& state[key] |
37
|
|
|
&& state[key].get |
38
|
|
|
&& state[key].get(entry) |
39
|
|
|
? state[key].get(entry) |
40
|
|
|
: null; |
41
|
|
|
|
42
|
|
|
if (firstTry) { |
43
|
|
|
return firstTry.toJS ? firstTry.toJS() : firstTry; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
const keys = Object.keys(state); |
47
|
|
|
const normalizedKeys = keys |
48
|
|
|
.map((s) => s.toLowerCase()); |
49
|
|
|
|
50
|
|
|
const keyIndex = normalizedKeys.indexOf(key.toLowerCase()); |
51
|
|
|
|
52
|
|
|
if (keyIndex !== -1) { |
53
|
|
|
const secondTry = state |
54
|
|
|
&& state[keys[keyIndex]] |
55
|
|
|
&& state[keys[keyIndex]].get |
56
|
|
|
&& state[keys[keyIndex]].get(entry) |
57
|
|
|
? state[keys[keyIndex]].get(entry) |
58
|
|
|
: null; |
59
|
|
|
|
60
|
|
|
if (!state[keys[keyIndex]]) { |
61
|
|
|
/* eslint-disable no-console */ |
62
|
|
|
console.warn([ |
63
|
|
|
'Case insensitivity for reducer keys will no longer', |
64
|
|
|
'be supported in the next major release.', |
65
|
|
|
'Please update your reducer keys', |
66
|
|
|
'to match the main exports.' |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return secondTry && secondTry.toJS |
71
|
|
|
? secondTry.toJS() |
72
|
|
|
: secondTry; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return null; |
76
|
|
|
} |
77
|
|
|
|